package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysWxUser;
import com.ruoyi.system.service.ISysWxUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 微信小程序用户Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/system/wxuser")
public class SysWxUserController extends BaseController
{
    @Autowired
    private ISysWxUserService sysWxUserService;

    /**
     * 查询微信小程序用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:wxuser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysWxUser sysWxUser)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysWxUser.setOwnerid("1");
        }else{
            sysWxUser.setOwnerid(user.getUserId()+"");
        }
        startPage();
        List<SysWxUser> list = sysWxUserService.selectSysWxUserList(sysWxUser);
        return getDataTable(list);
    }

    /**
     * 导出微信小程序用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:wxuser:export')")
    @Log(title = "微信小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysWxUser sysWxUser)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysWxUser.setOwnerid("1");
        }else{
            sysWxUser.setOwnerid(user.getUserId()+"");
        }
        List<SysWxUser> list = sysWxUserService.selectSysWxUserList(sysWxUser);
        ExcelUtil<SysWxUser> util = new ExcelUtil<SysWxUser>(SysWxUser.class);
        util.exportExcel(response, list, "微信小程序用户数据");
    }

    /**
     * 获取微信小程序用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:wxuser:query')")
    @GetMapping(value = "/{wxuserid}")
    public AjaxResult getInfo(@PathVariable("wxuserid") Long wxuserid)
    {
        return success(sysWxUserService.selectSysWxUserByWxuserid(wxuserid));
    }

    /**
     * 新增微信小程序用户
     */
    @PreAuthorize("@ss.hasPermi('system:wxuser:add')")
    @Log(title = "微信小程序用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysWxUser sysWxUser)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysWxUser.setOwnerid("1");
        }else{
            sysWxUser.setOwnerid(user.getUserId()+"");
        }
        return toAjax(sysWxUserService.insertSysWxUser(sysWxUser));
    }

    /**
     * 修改微信小程序用户
     */
    @PreAuthorize("@ss.hasPermi('system:wxuser:edit')")
    @Log(title = "微信小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysWxUser sysWxUser)
    {
        return toAjax(sysWxUserService.updateSysWxUser(sysWxUser));
    }

    /**
     * 删除微信小程序用户
     */
    @PreAuthorize("@ss.hasPermi('system:wxuser:remove')")
    @Log(title = "微信小程序用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wxuserids}")
    public AjaxResult remove(@PathVariable Long[] wxuserids)
    {
        return toAjax(sysWxUserService.deleteSysWxUserByWxuserids(wxuserids));
    }
}
